use snafu::Snafu;
use std::path::{Path, PathBuf};
use uuid::Uuid;

mod handler;

#[derive(Debug, Snafu)]
pub enum Error {
    #[snafu(whatever, display("{message}"))]
    Whatever {
        message: String,
        #[snafu(source(from(Box<dyn std::error::Error + Send + Sync>, Some)))]
        source: Option<Box<dyn std::error::Error + Sync + Send>>,
    },
}

pub type Result<T, E = Error> = std::result::Result<T, E>;

pub enum FileNode {
    Dir(Vec<FileNode>),
    File(PathBuf, Uuid, bool, u64),
}

pub fn build_tree(input_dir: &Path, max_file_size: Option<u64>) -> Result<FileNode> {
    handler::build_tree(input_dir, max_file_size)
}
pub fn print_index(root: &FileNode) -> Result<()> {
    handler::print_index(root)
}

pub fn print_context(
    base: &Path,
    root: &FileNode,
    include_exits: Vec<String>,
    exclude_exits: Vec<String>,
) -> Result<()> {
    handler::print_context(root, include_exits, exclude_exits, base)
}

pub fn print_prompt() {
    println!(
        r#"Dev:
This file is a codebase bundle generated by `ravel`.

Structure:
- INDEX: between $$$$INDEX-START$$$$ and $$$$INDEX-END$$$$.
  Each line: /path/to/file.rs->[uuid=xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx]
- CONTEXT: between $$$CONTEXT-START$$$ and $$$CONTEXT-END$$$.
  Each file block:
    @@@@FILE START@@@@-PATH=/path/to/file.rs&&UUID=...
    <original file content>
    @@@@FILE END@@@@-PATH=/path/to/file.rs&&UUID=...

For AI assistants:
- When the user mentions a file (e.g. "src/main.rs"), first find its line in INDEX and read the UUID.
- Then, in CONTEXT, read only the block between the matching FILE START/END markers for that PATH and UUID.
- Use only the relevant blocks to answer questions; you do not need to load the entire bundle at once.
"#
    );
}
